package gyb.rasa.controller;


import gyb.rasa.common.utils.Result;
import gyb.rasa.entity.Disease;
import gyb.rasa.entity.RasaMsg;
import gyb.rasa.entity.User;
import gyb.rasa.entity.vo.ChatAIRequest;
import gyb.rasa.entity.vo.GPT35Request;
import gyb.rasa.entity.vo.GPT3Request;
import gyb.rasa.mapper.DiseaseRepository;
import gyb.rasa.service.MessageReceivedService;
import gyb.rasa.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 郜宇博
 * @since 2023-04-02
 */
@RestController
@RequestMapping("/api")
public class UserController {
    private final UserService userService;
    private final MessageReceivedService messageReceivedService;
    private final DiseaseRepository diseaseRepository;

    public UserController(UserService userService, MessageReceivedService messageReceivedService, DiseaseRepository diseaseRepository) {
        this.userService = userService;
        this.messageReceivedService = messageReceivedService;
        this.diseaseRepository = diseaseRepository;
    }

    /**
     * 注册
     *
     * @param user 用户注册输入的信息
     * @return 无
     */
    @PostMapping("register")
    @ApiOperation("注册")
    public Result registerUser(@RequestBody User user) {
        userService.register(user);
        return Result.ok().message("注册成功");
    }

    /**
     * 登录
     *
     * @param user 用户登录信息
     * @return token
     */
    @ApiOperation("登录")
    @PostMapping("login")
    public Result loginUser(@RequestBody User user) {
        String token = userService.login(user);
        return Result.ok().data("token", token);
    }

    /**
     * 通过token获取用户信息
     *
     * @param request http请求
     * @return 用户信息
     */
    @ApiOperation("通过token获取用户信息")
    @GetMapping("userInfo")
    public Result getUserInfo(HttpServletRequest request) {
        User user = userService.getUserByToken(request);
        return Result.ok().data("userInfo", user);
    }

    @ApiOperation("Rasa对话")
    @PostMapping("rasaChat")
    public Result rasaChat(@RequestBody RasaMsg rasaMsg,HttpServletRequest request) {
        //获取响应
        ArrayList<Map<String,String>> response = userService.rasaChat2(rasaMsg);
        if (response != null){
            //存储会话到数据库
            int mid = messageReceivedService.addChat(rasaMsg, request, response);
            return Result.ok().data("RasaResponse", response).data("mid",mid);
        }
        return Result.error().message("发送失败");

    }
    @ApiOperation("GPT3.5外部接口")
    @PostMapping("chatGpt35")
    public Result chatGpt35(@RequestBody GPT35Request gpt35Request, HttpServletRequest httpServletRequest) {
        Map<String,Object> response = userService.chatGpt35(gpt35Request);
        return Result.ok().data(response);

    }
    @ApiOperation("GPT3外部接口")
    @PostMapping("chatGpt3")
    public Result chatGpt3(@RequestBody GPT3Request gpt3Request, HttpServletRequest httpServletRequest) {
        Map<String,Object> response = userService.chatGpt3(gpt3Request);
        return Result.ok().data(response);

    }
    @ApiOperation("全网搜索接口")
    @PostMapping("chatAI")
    public Result chatAI(@RequestBody ChatAIRequest chatAIRequest, HttpServletRequest httpServletRequest) {
        Map<String,Object> response = userService.ChatAI(chatAIRequest);
        return Result.ok().data(response);

    }
    @ApiOperation("获取疾病")
    @GetMapping("disease/{name}")
    public Result getDiseaseByName(@ApiParam(name = "name", value = "疾病名称",required = true) @PathVariable String name) {
        Disease disease = diseaseRepository.getDiseaseByName(name);
        return Result.ok().data("disease",disease);
    }

}

